package demo.hdz.mediacodecdecode.mainpage;

import android.content.Context;
import android.media.MediaCodecInfo;
import android.media.MediaCodecList;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

import demo.hdz.mediacodecdecode.AvcDecoder;
import demo.hdz.mediacodecdecode.Logger;
import demo.hdz.mediacodecdecode.SpsDecode;
import demo.hdz.mediacodecdecode.bean.VideoSizeBean;

/**
 * @author hdz
 * @date 2019/04/18
 */
public class MainModelImpl implements IMainModel {

    private byte[] getSpsData(Context context, String h264File, boolean bAssets) {
        InputStream inputStream = null;
        try {
            if (bAssets) {
                inputStream = context.getResources().getAssets().open(h264File);
            } else {
                File file = new File(h264File);
                inputStream = new FileInputStream(file);
            }

            byte[] headData = new byte[4];
            int ret = inputStream.read(headData, 0, headData.length);
            if (ret == headData.length) {
                ByteBuffer byteBuffer = ByteBuffer.wrap(Arrays.copyOf(headData, 4));
                byteBuffer.order(ByteOrder.LITTLE_ENDIAN);

                int dataLen = byteBuffer.getInt(0);
                Logger.d("AvcDecoder, dataLen: " + dataLen);

                byte[] inData = new byte[dataLen];
                ret = inputStream.read(inData, 0, inData.length);
                if (ret == inData.length) {
                    if ( (inData[4] & 0x1f) == 0x07) {
                        return inData;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        try {
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public VideoSizeBean getVideoSize(Context context, String filePath, boolean bAssets) {
        byte[] sps = getSpsData(context, filePath, bAssets);
        if (sps == null) {
            return null;
        }
        SpsDecode spsDecode = new SpsDecode();
        return spsDecode.decode(sps);
    }


    /**
     * 检查设备解码能力
     * */
    @Override
    public boolean checkHwDecoder() {
        boolean bFlag = false;
        int codecCount = MediaCodecList.getCodecCount();
        for (int i = 0; i  < codecCount; i++)  {
            MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
            String sInfo = "";
            if (codecInfo.isEncoder()) {
                sInfo += "ENCODER " + codecInfo.getName() + " ";
            } else {
                sInfo += "DECODER " + codecInfo.getName() + " ";
            }

            String[] types = codecInfo.getSupportedTypes();
            for (String sType : types) {
                if (sType.equalsIgnoreCase(AvcDecoder.MIME_TYPE)) {
                    if (!codecInfo.isEncoder()) {
                        Logger.d("checkCodec", "查找到支持 video/avc 的解码器");
                        bFlag = true;
                    }
                }
                Logger.d("checkCodec",  sInfo + sType);
            }
        }
        return bFlag;
    }
}
